|
1
|
|
|
import { |
|
2
|
|
|
BadRequestException, |
|
3
|
|
|
Body, |
|
4
|
|
|
Controller, |
|
5
|
|
|
Get, |
|
6
|
|
|
Inject, |
|
7
|
|
|
Param, |
|
8
|
|
|
Post, |
|
9
|
|
|
Render, |
|
10
|
|
|
Res, |
|
11
|
|
|
UseGuards |
|
12
|
|
|
} from '@nestjs/common'; |
|
13
|
|
|
import { Response } from 'express'; |
|
14
|
|
|
import { ICommandBus } from 'src/Application/ICommandBus'; |
|
15
|
|
|
import { IQueryBus } from 'src/Application/IQueryBus'; |
|
16
|
|
|
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO'; |
|
17
|
|
|
import { IsAuthenticatedGuard } from '../Security/IsAuthenticatedGuard'; |
|
18
|
|
|
import { WithName } from 'src/Infrastructure/Common/ExtendedRouting/WithName'; |
|
19
|
|
|
import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver'; |
|
20
|
|
|
import { UserRole } from 'src/Domain/HumanResource/User/User.entity'; |
|
21
|
|
|
import { |
|
22
|
|
|
ContractType, |
|
23
|
|
|
UserAdministrative, |
|
24
|
|
|
WorkingTimeType |
|
25
|
|
|
} from 'src/Domain/HumanResource/User/UserAdministrative.entity'; |
|
26
|
|
|
import { GetUserAdministrativeByIdQuery } from 'src/Application/HumanResource/User/Query/GetUserAdministrativeByIdQuery'; |
|
27
|
|
|
import { |
|
28
|
|
|
CreateUserCommand, |
|
29
|
|
|
IUserAdministrativeCommand |
|
30
|
|
|
} from 'src/Application/HumanResource/User/Command/CreateUserCommand'; |
|
31
|
|
|
import { UserDTO } from '../DTO/UserDTO'; |
|
32
|
|
|
|
|
33
|
|
|
@Controller('app/people/users/add') |
|
34
|
|
|
@UseGuards(IsAuthenticatedGuard) |
|
35
|
|
|
export class AddUserController { |
|
36
|
|
|
constructor( |
|
37
|
|
|
@Inject('ICommandBus') |
|
38
|
|
|
private readonly commandBus: ICommandBus, |
|
39
|
|
|
private readonly resolver: RouteNameResolver |
|
40
|
|
|
) {} |
|
41
|
|
|
|
|
42
|
|
|
@Get() |
|
43
|
|
|
@WithName('people_users_add') |
|
44
|
|
|
@Render('pages/users/add.njk') |
|
45
|
|
|
public async get() { |
|
46
|
|
|
const roles = Object.values(UserRole); |
|
47
|
|
|
const contracts = Object.values(ContractType); |
|
48
|
|
|
const workingTimes = Object.values(WorkingTimeType); |
|
49
|
|
|
|
|
50
|
|
|
return { roles, contracts, workingTimes }; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
@Post() |
|
54
|
|
|
public async post(@Body() userDto: UserDTO, @Res() res: Response) { |
|
55
|
|
|
try { |
|
56
|
|
|
const { |
|
57
|
|
|
firstName, |
|
58
|
|
|
lastName, |
|
59
|
|
|
email, |
|
60
|
|
|
password, |
|
61
|
|
|
role, |
|
62
|
|
|
...userAdministrative |
|
63
|
|
|
} = userDto; |
|
64
|
|
|
|
|
65
|
|
|
await this.commandBus.execute( |
|
66
|
|
|
new CreateUserCommand( |
|
67
|
|
|
firstName, |
|
68
|
|
|
lastName, |
|
69
|
|
|
email, |
|
70
|
|
|
password, |
|
71
|
|
|
role, |
|
72
|
|
|
userAdministrative |
|
73
|
|
|
) |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
res.redirect(303, this.resolver.resolve('people_users_list')); |
|
77
|
|
|
} catch (e) { |
|
78
|
|
|
throw new BadRequestException(e.message); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|